package net.tngou.iask.action.my;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.entity.Statistic;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.Statisticmedical;
import net.tngou.iask.service.AccessService;
import net.tngou.iask.service.CureService;
import net.tngou.iask.service.MedicalService;
import net.tngou.iask.service.StatisticService;

/**
 * 医学统计
 * @author tngou@tngou.net
 *
 */
public class MedicalAction extends BaseAction {

	/**
	 * 医学统计
	 */
	@Override
	public void execute() throws ServletException, IOException {
		MedicalService service = new MedicalService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain());	
		root.put("page", page);
		printFreemarker("my/medical.ftl", root);
	}
	
	/**
	 * 治疗统计
	 */
	public void cure() {
		if(ask.getId()==0){run_404();return;}
		Statisticmedical medical=new Statisticmedical().get(ask.getId());
		if(medical==null){run_404();return;}
		Disease disease = new Disease().get(medical.getDisease());
		if(disease==null){run_404();return;}
		StatisticService service = new StatisticService();
		Statistic statistic=service.getCure(user.getMain(),medical.getDisease());
		root.put("statistic", statistic);
		root.put("medical", medical);
		root.put("disease", disease);
		printFreemarker("my/medical_cure.ftl", root);
		
	}
	
	/**
	 * 随访统计
	 */
	public void access() {
		if(ask.getId()==0){run_404();return;}
		Statisticmedical medical=new Statisticmedical().get(ask.getId());
		if(medical==null){run_404();return;}
		Disease disease = new Disease().get(medical.getDisease());
		if(disease==null){run_404();return;}
		StatisticService service = new StatisticService();
		Statistic statistic =service.getAccess(user.getMain(),disease.getId());
		root.put("statistic", statistic);
		root.put("medical", medical);
		root.put("disease", disease);
		MedicalService medicalService = new MedicalService();
		float[] generationrates= new float[10];//即时十年生成率
		for (int i = 0; i < generationrates.length; i++) {
			generationrates[i]=medicalService.generationrate(user.getMain(),disease.getId(), i+1);
		}
		root.put("generationrates", generationrates);
		printFreemarker("my/medical_access.ftl", root);
	}
	
	/**
	 * 显示疾病的医学统计
	 */
	public void show() {	
		long id =ask.getId();
		if(id==0){run_404();return;}
		Statisticmedical medical = new Statisticmedical().get(id);
		if(medical==null){run_404();return;}
		Disease disease = new Disease().get(medical.getDisease());
		if(disease==null){run_404();return;}
		
		StatisticService service = new StatisticService();
		Statistic access =service.getAccess(user.getMain(),disease.getId());
		root.put("access", access);
		Statistic cure=service.getCure(user.getMain(),medical.getDisease());
		root.put("cure", cure);
		
		CureService cureService = new CureService();
		 PageUtil curepage = cureService.getPage(1, Integer.MAX_VALUE, user.getMain(), disease.getId());
		 root.put("curepage", curepage);
		
		 AccessService accessService = new  AccessService();
		 PageUtil accesspage = accessService .getPage(1, Integer.MAX_VALUE, user.getMain(), disease.getId());
		 root.put("accesspage",accesspage);
		root.put("medical", medical);
		root.put("disease", disease);
		printFreemarker("my/medical_show.ftl", root);
	}
}
